local assets =
{
    -- Asset("ANIM", "anim/panda_fisherman_supply_pack.zip"),
    -- Asset( "IMAGE", "images/inventoryimages/panda_fisherman_supply_pack.tex" ),  -- 背包贴图
    -- Asset( "ATLAS", "images/inventoryimages/panda_fisherman_supply_pack.xml" ),
}
local function GetStringsTable()
    return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["npc_item_transform_compass"] or {}
end

local function onequip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_object", "swap_compass", "swap_compass")
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")
    if owner.components.maprevealable ~= nil then
        owner.components.maprevealable:AddRevealSource(inst, "compassbearer")
    end
    owner:AddTag("compassbearer")
end

local function onunequip(inst, owner)
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
    if owner.components.maprevealable ~= nil then
        owner.components.maprevealable:RemoveRevealSource(inst)
    end
    owner:RemoveTag("compassbearer")
end

local function onequiptomodel(inst, owner, from_ground)
    if owner.components.maprevealable ~= nil then
        owner.components.maprevealable:RemoveRevealSource(inst)
    end
    owner:RemoveTag("compassbearer")
end

---------------------------------------------------------------------------------------------
local function player_back_2_door_check(player)
    local pt = Vector3( player.Transform:GetWorldPosition() )
    local ents = TheSim:FindEntities(pt.x, 0, pt.z, 5, {"multiplayer_portal"}, nil, nil)
    if #ents > 0 then
        return true        
    end
    return false
end
---------------------------------------------------------------------------------------------
local function spellfn(inst, target)
    local door = TheSim:FindFirstEntityWithTag("multiplayer_portal")
    local owner = inst.npc_base_lib:Get_Owner()
    if door and owner then
        local owner_pt = Vector3(owner.Transform:GetWorldPosition())
        local door_pt = Vector3(door.Transform:GetWorldPosition())
        SpawnPrefab("npc_fx_spawn"):PushEvent("Set",{pt = owner_pt})
        SpawnPrefab("npc_fx_spawn"):PushEvent("Set",{pt = door_pt})
        owner.Transform:SetPosition(door_pt.x,0,door_pt.z)
        owner.components.playercontroller:Enable(false)
        owner.______compass_trans_check_task = owner:DoPeriodicTask(0.1,function()
            if player_back_2_door_check(owner) == true then
                if owner.______compass_trans_check_task then
                    owner.______compass_trans_check_task:Cancel()
                    owner.______compass_trans_check_task = nil
                end
                owner.components.playercontroller:Enable(true)
            else
                print("compass trans error :")
                owner.Transform:SetPosition(door_pt.x,0,door_pt.z)
            end
        end)

        door.AnimState:PlayAnimation("fx")
        door.AnimState:HideSymbol("FX_ray1")
        door.AnimState:PushAnimation("idle_loop",true)
        door.AnimState:ShowSymbol("FX_ray1")
        inst:Remove()
    end
end
local function equipped_event_fn(inst)
    inst:AddComponent("spellcaster")
    inst.components.spellcaster:SetSpellFn(spellfn)
    inst.components.spellcaster.canusefrominventory = true
    inst.components.spellcaster.quickcast = true
end

local function unequipped_event_fn(inst)
    inst:RemoveComponent("spellcaster")    
end

---------------------------------------------------------------------------------------------

local function risk_taker_lead_event_init(inst) ---- 在洞穴里周期性的释放路标指向,方便救援
    if not TheWorld:HasTag("cave") then
        return
    end

    local function CreateFx(inst)
        local tent = TheSim:FindFirstEntityWithTag("npc_item_risk_taker_tent") 
        if tent and tent:IsValid() then
            tent:PushEvent("announce_event")
            local fx = SpawnPrefab("npc_projectile_rainbow")
            fx:PushEvent("Set",{
                pt = Vector3(inst.Transform:GetWorldPosition()),
                target = tent,
                owner = inst,
                target_fn = function(fx) fx:Remove() end,
                speed = 6,
            })
            fx:DoTaskInTime(10,fx.Remove)
        end
    end

    inst:DoTaskInTime(math.random(30,50),CreateFx)

    inst:WatchWorldState("cycles", function()
        inst:DoTaskInTime(math.random(30,50),CreateFx)
    end)

end
---------------------------------------------------------------------------------------------


local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("compass")
    inst.AnimState:SetBuild("compass")
    inst.AnimState:PlayAnimation("idle", true)

    inst:AddTag("compass")
    inst:AddTag("quickcast")
    inst:AddTag("npc_item_transform_compass")
    --   需要 inst.components.spellcaster.quickcast = true 同步支持tag 才能快速施法，单纯加tag不行
  

    MakeInventoryFloatable(inst, "med", 0.1, 0.6)

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent("npc_base_lib")
    -- inst:AddComponent("npc_everything_data")


    inst:AddComponent("inspectable") --可检查组件
    
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem:ChangeImageName("compass")
	inst.components.inventoryitem.cangoincontainer = true
    inst:AddComponent("named")
    inst.components.named:SetName(GetStringsTable().name)
    STRINGS.CHARACTERS.GENERIC.DESCRIBE[string.upper("npc_item_transform_compass")] = GetStringsTable().inspect_str  --人物检查的描述

    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)
    inst.components.equippable:SetOnEquipToModel(onequiptomodel)
    inst.components.equippable.equipslot = EQUIPSLOTS.HANDS

    inst:AddComponent("tradable")
    
    inst:ListenForEvent("equipped",function(_,_table)
        if _table and _table.owner and _table.owner:HasTag("player") and _table.owner.components.playercontroller then
            equipped_event_fn(inst)
        end        
    end)

    inst:ListenForEvent("unequipped",function(_,_table)
        if _table and _table.owner and _table.owner:HasTag("player") and _table.owner.components.playercontroller then
            unequipped_event_fn(inst)
        end        
    end)

    risk_taker_lead_event_init(inst)
    MakeHauntableLaunch(inst)    
    return inst
end

return Prefab("npc_item_transform_compass", fn,assets)